//  接口
// 方法对于json对象的约束
function printLabel(labelInfo:{label:string}):string{
    return labelInfo.label
}

console.log(printLabel({'label':'帅哥'}))

//  属性接口 可选属性
// 方法传入对象的约束 
//  规定传入的对象的标准
interface fullName{
    firstName:string,
    secondName:string,
    age?:number         //  接口的可选属性 可传可不传                                                                                                                                                                              
}
function printName(name:fullName):string{
    if(name.age){
        return `${name.firstName}---${name.secondName}---${name.age}`
    }else{
        return name.firstName+name.secondName
    }
}
// 在外部定义对象传入 只要含有firstName和secondName 可以含有其他属性
const obj = {
    firstName:"尼古拉斯",
    secondName:"赵四",
    age:21
}
//这样直接传入对象 需要只能包含firstName和secondName 不能包含其他属性
console.log(printName({firstName:'尼古拉斯',secondName:'二狗'}))
console.log(printName(obj))

// ajax的例子
interface config{
    type:string,
    url:string,
    data?:string,
    dataType:string
}
function ajax(config:config){
    var xhr = new XMLHttpRequest()
    xhr.open(config.type,config.url,true)
    xhr.send(config.data)
    console.log(config.data)
    xhr.onreadystatechange = function(){
        //console.log(xhr.responseText)
        if(xhr.readyState==4&&xhr.status==200){
            console.log('success')
        }
        if(config.dataType === 'json'){
            JSON.parse(xhr.responseText)
        }else{
            console.log(xhr.responseText)
        }
    }
}
const conf = {
    type:'get',
    url:'https://www.mxnzp.com/api/image/girl/list/random',
    dataType:'json'
}
ajax(conf)

//  函数接口
//  定义函数规范的接口
interface encrypt{
    (key:string,value:string):string
}
var md5:encrypt = function(key1:string,value1:string):string{
    return key1+'---'+value1
}
console.log(md5('ywj','最帅'))


//  可索引接口
//  规定了数组的规范
interface UserArr{
    //  索引为number类型 每一项为string类型
    [index:number]:string
}
var arr1:UserArr = ['123','12']
console.log(arr1,arr1[0])

//  可索引接口
//  规定了对象的规范
interface userObj{
    [index:string]:string
}
var obj2:userObj = {user:'ywj',age:'20'}
console.log(obj2)
console.log(obj2.user)

//  类类型接口
//  定义类的规范
interface Animals{
    name:string;
    eat(food:string):string;
}
class Dogs implements Animals{
    public name:string
    constructor(name:string){
        this.name = name
    }
    drink(water:string):string{
        return `${this.name}喝${water}`
    }
    eat(food:string):string{
        return `${this.name}吃${food}`
    }
}
const dogs = new Dogs('xiaoming')
console.log(dogs.eat('狗粮')) 

//  接口的继承
//  Persons类继承了Animals接口
interface Persons extends Animals{
    age:number
    work(job:string):string
}

class Your {
    str:string
    constructor(str:string){
        this.str = str
    }
    drink(water:string):string{
        return `${this.str}喝${water}`
    }
}
//  实现了Persons 需要重写Persons和Animals的方法
//  类可以实现接口和继承父类
class Mine extends Your implements Persons{
    public name:string
    public age:number
    constructor(str:string,name:string,age:number){
        super(str)
        this.name = name
        this.age = age
    }
    eat(food:string):string{
        return `${this.name}---${this.age}---吃${food}`
    }
    work(job:string):string{
        return `${this.name}---${this.age}---${job}`
    }
}
const mine = new Mine('二狗','ywj',20);
console.log(mine.eat('饭'))
console.log(mine.work('打代码'))
console.log(mine.drink('水'))
